package ken.app.adapter;

import java.lang.ref.SoftReference;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ken.app.bean.QueryPersonCouponBean;
import ken.app.bean.SportBabyListBean;
import ken.app.bean.TreeSingleBean;
import ken.app.helper.Helper;
import com.she.olympic.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.she.component.adapter.GenView;
import com.she.component.adapter.GeneralAdapter;
import com.she.component.adapter.iface.IGenView;
import com.she.olympic.TreeActivity;

public class SportBabyListAdapter extends GeneralAdapter {
	
	static ArrayList<SportBabyListBean> mDataList;
	static Handler mhandler;
	static GenSelectAllListItemView mGenSelectAllView = new GenSelectAllListItemView();
	
	public SportBabyListAdapter(Context context, List<?> dataList ,Handler handler) {
		super(context, mGenSelectAllView);
		mDataList = (ArrayList<SportBabyListBean>) dataList;
		mhandler = handler;
		this.setData(mDataList);
	}
	
	
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
//		return super.getCount();
		if (mDataList.size() == 0) {
			return 0;
		}else if (mDataList.size() < 6) {
			return 1;
		}else{
			int c;
			if (mDataList.size() % 6 > 0) {
				c = 1;
			}else{
				c = 0;
			}
			return (mDataList.size() / 6) + c;
		}
	}



	public static class GenSelectAllListItemView extends GenView{
		LayoutInflater inflater;
		private Map<String, SoftReference<Bitmap>> imageCache=new HashMap<String, SoftReference<Bitmap>>();
		@Override
		protected View genBody(Context context, View convertView,
				final Object element, final int position) {
						
			if (inflater == null) {
				inflater = LayoutInflater.from(context);
			}
			final ViewHolder holder;
			if (convertView != null	&& convertView.getTag() instanceof ViewHolder) 
			{
				holder = (ViewHolder) convertView.getTag();
			} else 
			{
				convertView = inflater.inflate(R.layout.babylist_row, null);
				holder = new ViewHolder();
				holder.img1 = (ImageView) convertView.findViewById(R.id.babylist_img1);
				holder.img2 = (ImageView) convertView.findViewById(R.id.babylist_img2);
				holder.img3 = (ImageView) convertView.findViewById(R.id.babylist_img3);
				holder.img4 = (ImageView) convertView.findViewById(R.id.babylist_img4);
				holder.img5 = (ImageView) convertView.findViewById(R.id.babylist_img5);
				holder.img6 = (ImageView) convertView.findViewById(R.id.babylist_img6);
				holder.txt1 = (TextView) convertView.findViewById(R.id.babylist_txt1);
				holder.txt2 = (TextView) convertView.findViewById(R.id.babylist_txt2);
				holder.txt3 = (TextView) convertView.findViewById(R.id.babylist_txt3);
				holder.txt4 = (TextView) convertView.findViewById(R.id.babylist_txt4);
				holder.txt5 = (TextView) convertView.findViewById(R.id.babylist_txt5);
				holder.txt6 = (TextView) convertView.findViewById(R.id.babylist_txt6);
				convertView.setTag(holder);
			}
			
			int count =  mDataList.indexOf((SportBabyListBean)element);
			/*顯示寶寶圖*/
			BitmapFactory.Options opt = new BitmapFactory.Options();
			opt.inPreferredConfig = Bitmap.Config.ALPHA_8;
			opt.inPurgeable = true;
			opt.inInputShareable = true;
			try {
				BitmapFactory.Options.class.getField("inNativeAlloc").setBoolean(opt,true);
			} catch (IllegalArgumentException e1) {
				e1.printStackTrace();
			} catch (SecurityException e1) {
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				e1.printStackTrace();
			} catch (NoSuchFieldException e1) {
				e1.printStackTrace();
			}
//			try {
//				Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/.olympic/" + mDataList.get(6*count).getShowImg() + ".png", opt);
//				if (bitmap == null) {
//					holder.img1.setImageResource(Helper.getResId(context , "drawable" , mDataList.get(6*count).getShowImg()));
//				}else{
//					holder.img1.setImageBitmap(bitmap);
//				}
//				bitmap = null;
//			} catch (Exception e) {	holder.img1.setImageDrawable(null); }
//			try {
//				Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/.olympic/" + mDataList.get((6*count)+1).getShowImg() + ".png", opt);
//				if (bitmap == null) {
//					holder.img2.setImageResource(Helper.getResId(context , "drawable" , mDataList.get((6*count)+1).getShowImg()));
//				}else{
//					holder.img2.setImageBitmap(bitmap);
//				}
//				bitmap = null;
//			} catch (Exception e) {	holder.img2.setImageDrawable(null); }
//			try {
//				Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/.olympic/" + mDataList.get((6*count)+2).getShowImg() + ".png", opt);
//				if (bitmap == null) {
//					holder.img3.setImageResource(Helper.getResId(context , "drawable" , mDataList.get((6*count)+2).getShowImg()));
//				}else{
//					holder.img3.setImageBitmap(bitmap);
//				}
//				bitmap = null;
//			} catch (Exception e) {	holder.img3.setImageDrawable(null); }
//			try {
//				Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/.olympic/" + mDataList.get((6*count)+3).getShowImg() + ".png", opt);
//				if (bitmap == null) {
//					holder.img4.setImageResource(Helper.getResId(context , "drawable" , mDataList.get((6*count)+3).getShowImg()));
//				}else{
//					holder.img4.setImageBitmap(bitmap);
//				}
//				bitmap = null;
//			} catch (Exception e) {	holder.img4.setImageDrawable(null); }
//			try {
//				Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/.olympic/" + mDataList.get((6*count)+4).getShowImg() + ".png", opt);
//				if (bitmap == null) {
//					holder.img5.setImageResource(Helper.getResId(context , "drawable" , mDataList.get((6*count)+4).getShowImg()));
//				}else{
//					holder.img5.setImageBitmap(bitmap);
//				}
//				bitmap = null;
//			} catch (Exception e) {	holder.img5.setImageDrawable(null); }
//			try {
//				Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/.olympic/" + mDataList.get((6*count)+5).getShowImg() + ".png", opt);
//				if (bitmap == null) {
//					holder.img6.setImageResource(Helper.getResId(context , "drawable" , mDataList.get((6*count)+5).getShowImg()));
//				}else{
//					holder.img6.setImageBitmap(bitmap);
//				}
//				bitmap = null;
//			} catch (Exception e) {	holder.img6.setImageDrawable(null); }
			
			//================softReference
			try {
				SoftReference<Bitmap> softReference = imageCache.get(mDataList.get(6*count).getShowImg());
				Bitmap bitmap = null ;
				if (softReference != null) {
					bitmap = softReference.get();
				}
				if (bitmap == null) {
					bitmap = BitmapFactory.decodeFile("/sdcard/.olympic/" + mDataList.get(6*count).getShowImg() + ".png", opt);
					if (bitmap == null) {
						bitmap = BitmapFactory.decodeResource(context.getResources(), Helper.getResId(context , "drawable" , mDataList.get(6*count).getShowImg()));
					}
						imageCache.put(mDataList.get(6*count).getShowImg(), new SoftReference<Bitmap>(bitmap));
					holder.img1.setImageBitmap(bitmap);
					bitmap = null;
				}else{
					holder.img1.setImageBitmap(bitmap);
				}
			} catch (Exception e) {	holder.img1.setImageDrawable(null); }
			try {
				SoftReference<Bitmap> softReference = imageCache.get(mDataList.get((6*count)+1).getShowImg());
				Bitmap bitmap = null ;
				if (softReference != null) {
					bitmap = softReference.get();
				}
				if (bitmap == null) {
					bitmap = BitmapFactory.decodeFile("/sdcard/.olympic/" + mDataList.get((6*count)+1).getShowImg() + ".png", opt);
					if (bitmap == null) {
						bitmap = BitmapFactory.decodeResource(context.getResources(), Helper.getResId(context , "drawable" , mDataList.get((6*count)+1).getShowImg()));
					}
					imageCache.put(mDataList.get((6*count)+1).getShowImg(), new SoftReference<Bitmap>(bitmap));
					holder.img2.setImageBitmap(bitmap);
					bitmap = null;
				}else{
					holder.img2.setImageBitmap(bitmap);
				}
			} catch (Exception e) {	holder.img2.setImageDrawable(null); }
			try {
				SoftReference<Bitmap> softReference = imageCache.get(mDataList.get((6*count)+2).getShowImg());
				Bitmap bitmap = null ;
				if (softReference != null) {
					bitmap = softReference.get();
				}
				if (bitmap == null) {
					bitmap = BitmapFactory.decodeFile("/sdcard/.olympic/" + mDataList.get((6*count)+2).getShowImg() + ".png", opt);
					if (bitmap == null) {
						bitmap = BitmapFactory.decodeResource(context.getResources(), Helper.getResId(context , "drawable" , mDataList.get((6*count)+2).getShowImg()));
					}
						imageCache.put(mDataList.get((6*count)+2).getShowImg(), new SoftReference<Bitmap>(bitmap));
					holder.img3.setImageBitmap(bitmap);
					bitmap = null;
				}else{
					holder.img3.setImageBitmap(bitmap);
				}
			} catch (Exception e) {	holder.img3.setImageDrawable(null); }
			try {
				SoftReference<Bitmap> softReference = imageCache.get(mDataList.get((6*count)+3).getShowImg());
				Bitmap bitmap = null ;
				if (softReference != null) {
					bitmap = softReference.get();
				}
				if (bitmap == null) {
					bitmap = BitmapFactory.decodeFile("/sdcard/.olympic/" + mDataList.get((6*count)+3).getShowImg() + ".png", opt);
					if (bitmap == null) {
						bitmap = BitmapFactory.decodeResource(context.getResources(), Helper.getResId(context , "drawable" , mDataList.get((6*count)+3).getShowImg()));
					}
						imageCache.put(mDataList.get((6*count)+3).getShowImg(), new SoftReference<Bitmap>(bitmap));
					holder.img4.setImageBitmap(bitmap);
					bitmap = null;
				}else{
					holder.img4.setImageBitmap(bitmap);
				}
			} catch (Exception e) {	holder.img4.setImageDrawable(null); }
			try {
				SoftReference<Bitmap> softReference = imageCache.get(mDataList.get((6*count)+4).getShowImg());
				Bitmap bitmap = null ;
				if (softReference != null) {
					bitmap = softReference.get();
				}
				if (bitmap == null) {
					bitmap = BitmapFactory.decodeFile("/sdcard/.olympic/" + mDataList.get((6*count)+4).getShowImg() + ".png", opt);
					if (bitmap == null) {
						bitmap = BitmapFactory.decodeResource(context.getResources(), Helper.getResId(context , "drawable" , mDataList.get((6*count)+4).getShowImg()));
					}
						imageCache.put(mDataList.get((6*count)+4).getShowImg(), new SoftReference<Bitmap>(bitmap));
					holder.img5.setImageBitmap(bitmap);
					bitmap = null;
				}else{
					holder.img5.setImageBitmap(bitmap);
				}
			} catch (Exception e) {	holder.img5.setImageDrawable(null); }
			try {
				SoftReference<Bitmap> softReference = imageCache.get(mDataList.get((6*count)+5).getShowImg());
				Bitmap bitmap = null ;
				if (softReference != null) {
					bitmap = softReference.get();
				}
				if (bitmap == null) {
					bitmap = BitmapFactory.decodeFile("/sdcard/.olympic/" + mDataList.get((6*count)+5).getShowImg() + ".png", opt);
					if (bitmap == null) {
						bitmap = BitmapFactory.decodeResource(context.getResources(), Helper.getResId(context , "drawable" , mDataList.get((6*count)+5).getShowImg()));
					}
						imageCache.put(mDataList.get((6*count)+5).getShowImg(), new SoftReference<Bitmap>(bitmap));
					holder.img6.setImageBitmap(bitmap);
					bitmap = null;
				}else{
					holder.img6.setImageBitmap(bitmap);
				}
			} catch (Exception e) {	holder.img6.setImageDrawable(null); }
			//================
			/*顯示寶寶隻數*/
			try {
				if (mDataList.get(6*count).getBabyNum() > 0) { holder.txt1.setText("X"+ mDataList.get(6*count).getBabyNum()); }
				else{holder.txt1.setText("");}
			} catch (Exception e) {
				holder.txt1.setText("");
			}
			try {
				if (mDataList.get((6*count)+1).getBabyNum() > 0) { holder.txt2.setText("X"+ mDataList.get((6*count)+1).getBabyNum()); }
				else{holder.txt2.setText("");}
			} catch (Exception e) {
				holder.txt2.setText("");
			}
			try {
				if (mDataList.get((6*count)+2).getBabyNum() > 0) { holder.txt3.setText("X"+ mDataList.get((6*count)+2).getBabyNum()); }
				else{holder.txt3.setText("");}
			} catch (Exception e) {
				holder.txt3.setText("");
			}
			try {
				if (mDataList.get((6*count)+3).getBabyNum() > 0) { holder.txt4.setText("X"+ mDataList.get((6*count)+3).getBabyNum()); }
				else{holder.txt4.setText("");}
			} catch (Exception e) {
				holder.txt4.setText("");
			}
			try {
				if (mDataList.get((6*count)+4).getBabyNum() > 0) { holder.txt5.setText("X"+ mDataList.get((6*count)+4).getBabyNum()); }
				else{holder.txt5.setText("");}
			} catch (Exception e) {
				holder.txt5.setText("");
			}
			try {
				if (mDataList.get((6*count)+5).getBabyNum() > 0) { holder.txt6.setText("X"+ mDataList.get((6*count)+5).getBabyNum()); }
				else{holder.txt6.setText("");}
			} catch (Exception e) {
				holder.txt6.setText("");
			}
			
			return convertView;
		}
		
		private class ViewHolder {
			ImageView img1;
			ImageView img2;
			ImageView img3;
			ImageView img4;
			ImageView img5;
			ImageView img6;
			TextView txt1;
			TextView txt2;
			TextView txt3;
			TextView txt4;
			TextView txt5;
			TextView txt6;
		}
	}
	
}
